package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;

import java.util.List;

/**
 * 书架适配器
 * Created by wcy on 2017\6\19 0019
 */
public class BookShelfAdapter extends BaseQuickAdapter<BookInfo> {
    private boolean isEditable;

    public BookShelfAdapter(Context context, List<BookInfo> data) {
        super(context, data);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_shelf_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, final BookInfo item, final int position) {
        ImageView imageView = holder.getView(R.id.cover);
        if (BookInfo.SHELF_ADD_FLAG.equals(item.book_id)) {
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            holder.setImageResource(R.id.cover, R.mipmap.ic_add_shelf_btn_bg);
            holder.setBackgroundRes(R.id.cover, R.drawable.ic_book_cover_bg);
            DisplayUtils.visible(holder.getView(R.id.card));
            DisplayUtils.invisible(holder.getView(R.id.book_title), holder.getView(R.id.sort_label), holder.getView(R.id.checkbox));
        } else if (BookInfo.SHELF_EMPTY_FLAG.equals(item.book_id)) {
            holder.setImageResource(R.id.cover, 0);
            holder.setBackgroundRes(R.id.cover, 0);
            DisplayUtils.invisible(holder.getView(R.id.card), holder.getView(R.id.book_title), holder.getView(R.id.sort_label), holder.getView(R.id.checkbox));
        } else {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.setBackgroundRes(R.id.cover, R.drawable.ic_book_cover_bg);
            DisplayUtils.visible(holder.getView(R.id.card), holder.getView(R.id.book_title), holder.getView(R.id.checkbox));
            if (!TextUtils.isEmpty(item.cover_url) && item.cover_url.equals("cover")) {
                GlideImageLoader.load(R.mipmap.cover, holder.getView(R.id.cover));
            } else {
                GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
            }
        }
        DisplayUtils.gone(holder.getView(R.id.download_chapter_count));
        if (isEditable) {
            DisplayUtils.visible(holder.getView(R.id.checkbox_select));
        } else {
            DisplayUtils.invisible(holder.getView(R.id.checkbox_select));
            // 下载信息
            int download_count = MMKVUserManager.getInstance().getBookDownloadChapterCount(item.book_id);
            if(download_count != 0){
                DisplayUtils.visible(holder.getView(R.id.download_chapter_count));
                holder.setText(R.id.download_chapter_count,  "已下载"  + (TextUtils.equals(String.valueOf(download_count), item.chapter_count) ? "" : download_count + "章"));
            }
        }
        //阅读进度 及 书籍信息
        holder.setText(R.id.book_title, item.book_title);
        String bookLabel = BookUtils.getBookLabel(item);
        if (!TextUtils.isEmpty(bookLabel)) {
            holder.setText(R.id.sort_label, bookLabel);
            DisplayUtils.visible(holder.getView(R.id.sort_label));
        }
        // 编辑模式
        final CheckBox checkBox = holder.getView(R.id.checkbox);
        checkBox.setChecked(false);
        checkBox.setFocusable(false);
        if (isEditable) {
            if (isInvalidBook(item.book_id)) {
                DisplayUtils.invisible(checkBox);
            } else {
                DisplayUtils.visible(checkBox);
                holder.getView(R.id.checkbox_select).setVisibility(item.is_selected ? View.GONE : View.VISIBLE);
                checkBox.setChecked(item.is_selected);
            }
        } else {
            DisplayUtils.invisible(checkBox);
        }
        TextView label = holder.getView(R.id.label);
        if (item.is_updated) {
            DisplayUtils.visible(label);
            label.setText("更新");
        } else {
            DisplayUtils.invisible(label);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView != null && !isEditable && position == 0) {
            return HEADER_VIEW;
        } else if ((mData.size() + getHeaderViewsCount() + getFooterViewsCount()) == 0 && mEmptyView != null) {
            return EMPTY_VIEW;
        } else if (mIsLoadMoreEnable) {
            if (position == (getItemCount() - 1)) {
                return LOADING_VIEW;
            } else if (mFooterView != null && position == (getItemCount() - 2)) {
                return FOOTER_VIEW;
            }
        } else if (mFooterView != null && position == (getItemCount() - 1)) {
            return FOOTER_VIEW;
        }

        return getDefItemViewType(position - getHeaderViewsCount());
    }

    @Override
    public int getItemCount() {
        int all = super.getItemCount();
        if (isEditable) {
            all--;
        }
        return all;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mParentView == null) {
            mParentView = parent;
        }
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case LOADING_VIEW:
                baseViewHolder = new BaseViewHolder(mLoadingView);
                break;
            case HEADER_VIEW:
                baseViewHolder = new BaseViewHolder(mHeaderView);
                break;
            case EMPTY_VIEW:
                baseViewHolder = new BaseViewHolder(mEmptyView);
                break;
            case FOOTER_VIEW:
                baseViewHolder = new BaseViewHolder(mFooterView);
                break;
            case EMPTY_CONTAINER:
                View view = mLayoutInflater.inflate(R.layout.empty_container, parent, false);
                baseViewHolder = new BaseViewHolder(view);
                break;
            default:
                baseViewHolder = onCreateDefViewHolder(parent, viewType);
                // 设置用于单项刷新的tag标识
                baseViewHolder.itemView.setTag(R.id.view_holder_tag, baseViewHolder);
                _initItemClickListener(baseViewHolder);
                break;
        }
        return baseViewHolder;
    }

    public void setEditable(boolean editable) {
        isEditable = editable;
        notifyDataSetChanged();
    }

    public boolean isEditable() {
        return isEditable;
    }

    /**
     * 是否是有效书籍
     *
     * @param book_id
     * @return
     */
    public static boolean isInvalidBook(String book_id) {
        if (TextUtils.isEmpty(book_id) || BookInfo.SHELF_ADD_FLAG.equals(book_id)
                || BookInfo.SHELF_EMPTY_FLAG.equals(book_id)) {
            return true;
        }
        return false;
    }


    @Override
    public int getHeaderViewsCount() {
        return isEditable ? 0 : super.getHeaderViewsCount();
    }
}
